package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.SelectResultVo;
import com.zb.entity.SelectVo;
import com.zb.entity.ZjNetworkEmpLevel;
import com.zb.mapper.ZjNetworkEmpLevelMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjNetworkEmpLevelService;
import com.zb.vo.NetworkEmpLevelListVo;
import com.zb.vo.NetworkEmpLevelVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网点人员等级表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-29
 */
@Service
public class ZjNetworkEmpLevelServiceImpl extends ServiceImpl<ZjNetworkEmpLevelMapper, ZjNetworkEmpLevel> implements ZjNetworkEmpLevelService {
    @Resource
    private ZjNetworkEmpLevelMapper networkEmpLevelMapper;

    // 新增或更新人员等级
    @Override
    public void save(Long custAccId, NetworkEmpLevelVo empLevelVo) {
        Assert.isNull(networkEmpLevelMapper.existByName(custAccId, empLevelVo.getId(), empLevelVo.getName()), "人员等级名称已重复："+empLevelVo.getName());
        ZjNetworkEmpLevel empLevel = new ZjNetworkEmpLevel();
        BeanUtil.copyProperties(empLevelVo, empLevel);
        empLevel.setDisabled(empLevelVo.getStatus());
        if (null != empLevel.getId()) {
            this.updateById(empLevel);
            return;
        }
        empLevel.setId(IdWorker.getId());
        empLevel.setCustAccId(custAccId);
        this.save(empLevel);
    }

    // 获取人员等级列表
    @Override
    public Map<String, Object> getList(Long custAccId, SelectVo selectVo) {
        PageUtil.setPageLimit(selectVo);
        QueryWrapper<ZjNetworkEmpLevel> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "is_disabled AS disabled", "gmt_create", "gmt_update")
                .eq("cust_acc_id", custAccId)
                .orderByDesc("gmt_update");
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        Page<ZjNetworkEmpLevel> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords().stream().map(level -> {
            NetworkEmpLevelListVo vo = new NetworkEmpLevelListVo();
            BeanUtil.copyProperties(level, vo);
            vo.setStatus(level.getDisabled());
            vo.setCreateTime(level.getGmtCreate());
            vo.setUpdateTime(level.getGmtUpdate());
            return vo;
        }).toList());
    }

    // 根据ID获取人员等级详情
    @Override
    public NetworkEmpLevelVo getDetail(Long id) {
        ZjNetworkEmpLevel empLevel = this.getById(id);
        Assert.notNull(empLevel, "人员等级参数[ "+id+" ]的数据不存在");
        NetworkEmpLevelVo vo = new NetworkEmpLevelVo();
        BeanUtil.copyProperties(empLevel, vo);
        vo.setStatus(empLevel.getDisabled());
        return vo;
    }

    // 批量启用或禁用人员等级
    @Override
    public void updateDisabledBatchById(List<Long> ids, boolean status) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        networkEmpLevelMapper.updateDisabledBatchById(ids, status);
    }

    // 根据人员等级ID批量删除
    @Override
    public void deleteBatchById(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        this.removeByIds(ids);
    }

    // 获取服务商人员等级下拉数据
    @Override
    public Map<String, Object> getNetworkEmpLevelSelect(Long custAccId, SelectVo selectVo) {
        QueryWrapper<ZjNetworkEmpLevel> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name")
                .eq("cust_acc_id", custAccId).eq("is_disabled", false);
        if (StringUtils.isNotBlank(selectVo.getName())) {
            wrapper.like("name", selectVo.getName());
        }
        PageUtil.setSelectPageLimit(selectVo);
        Page<ZjNetworkEmpLevel> page = this.page(new Page<>(selectVo.getPage(), selectVo.getLimit()), wrapper);
        return PageUtil.pageList(page.getCurrent(), page.getSize(), page.getTotal(), page.getRecords().stream().map(level -> {
            return new SelectResultVo(level.getId(), level.getName());
        }).toList());
    }
}
